import { Component, OnInit } from '@angular/core';
import {ExtendHttpService} from "../../../services/extend-http.service";
import {TipService} from "../../../services/tip.service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-today-earnings-list',
  templateUrl: './today-earnings-list.component.html',
  styleUrls: ['./today-earnings-list.component.css']
})
export class TodayEarningsListComponent implements OnInit {
  tipType = 'error';
  profitFlag = false;
  profit = {profit: '', list: []}
  constructor(
    private extendHttp: ExtendHttpService,
    private tipService: TipService,
    private router: Router,
  ) { }

  ngOnInit() {
    cBack =()=> {
      $('#btn-back').trigger('click');
    }
    this.getData();
  }
  getData() {
    this.extendHttp.get('/agentApp/today', {}).subscribe(response => {
      let res = response.json();
      if (res['code'] === '0') {
        this.profit.list = res.list;
        res.list.forEach(k=>{
          k.expectedEarnings = (k.expectedEarnings).toFixed(8)
        });
        if(!res.list.length) {
          this.profitFlag = true;
        }
        this.profit.profit = res.profit;
      } else if (res['code'] == '-2001') {
        this.tipType = 'error';
        this.tipService.change(true, res['message']);
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 1500);
      } else {
        this.tipType = 'error';
        this.tipService.change(true, res['message']);
      }
    }, error => {
      this.tipType = 'error';
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

}
